

/*<remove trigger="prod">*/
import {geocoder,getWeather} from '../../lib/api-mock'
/*</remove>*/

/*<jdists trigger="prod">
import {geocoder,getWeather} from '../../lib/api'
</jdists>*/


//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    oneWord:'',
    lifeStyle:[],
    weeklyData:[],
    hourlyData: [],
    paddingTop:0,
    backgroundImage: '../../images/cloud.jpg',
    backgroundColor: '#62aadc',
    air: {
        status: 0,
        aqi: "77",
        color: "#00cf9a",
        name: "良"
      },
      current: {
        backgroundImage: "https://tianqi-1d3bf9.tcb.qcloud.la/bg/day/overcast.jpg",
        backgroundColor: "#5c7a93",
        temp: "35",
        wind: "南风",
        windLevel: "1",
        weather: "阴",
        humidity: "73",
        icon: "yin",
        ts: "2018-08-12 14:54"
      },
      today: {
        temp: "24/30°",
        icon: "leizhenyu",
        weather: "雷阵雨"
      },
      tomorrow: {
        temp: "24/30°",
        icon: "leizhenyu",
        weather: "雷阵雨"
      },
  },
  onLoad: function() {
    this.getLocation()
    let arr = [];
    for(let i =0;i < 24;i ++){
        arr.push(
            {
                temp: "29",
                time: "16:00",
                weather: "雷阵雨",
                icon: "leizhenyu"
            }
        )
    }
    let arr1 = [];
    for(let i = 0;i < 7;i ++){
        arr1.push(
            {
                day: "雷阵雨",
                dayIcon: "leizhenyu",
                dayWind: "南风",
                dayWindLevel: "1-2",
                maxTemp: "30",
                minTemp: "24",
                night: "中雨",
                nightIcon: "zhenyuye",
                nightWind: "南风",
                nightWindLevel: "1-2",
                time: 1534032000000
              }
        )
    }
    let arr2 = [];
    for(let i = 0; i< 10;i ++){
        arr2.push(
            {
                name: "舒适度", // 指数名称
                icon: "guominzhishu", // 指数对应的icon图标type
                info: "较不舒适", // 指数数值
                // 指数的详情
                detail: "白天虽然有雨，但仍无法削弱较高气温带来的暑意，同时降雨造成湿度加大会您感到有些闷热，不很舒适。"
              }
        )
    }
    this.setData({
        hourlyData:arr,
        weeklyData:arr1,
        lifeStyle:arr2,
    })

    wx.getSystemInfo({
        success: (res) => {
          // 状态栏高度和屏幕宽度

          this.setData({
            paddingTop: res.statusBarHeight+12
          })
        }
      })


  },
  render:function(data) {
    // isUpdate = true
    console.log(data)
    // const {width, scale} = this.data
    const { current, oneWord} = data
    // const {backgroundColor, backgroundImage} = current

    // const _today = daily[0],
    //   _tomorrow = daily[1]
    // const today = {
    //   temp: `${_today.minTemp}/${_today.maxTemp}°`,
    //   icon: _today.dayIcon,
    //   weather: _today.day
    // }
    // const tomorrow = {
    //   temp: `${_tomorrow.minTemp}/${_tomorrow.maxTemp}°`,
    //   icon: _tomorrow.dayIcon,
    //   weather: _tomorrow.day
    // }

    this.setData({
      // hourlyData: hourly,
      // weeklyData: daily,
      current,
      // backgroundImage,
      // backgroundColor,
      // today,
      // tomorrow,
      oneWord,
      // lifeStyle
    })

  },
  indexDetail:function(){

  },

  getWeatherData:function(cb){
    wx.showLoading({
      title: '获取数据中',
      mask: true
    })
    const fail = (e) => {
      wx.hideLoading()
      if (typeof cb === 'function') {
        cb()
      }
      // console.log(e.message, e)
      wx.showToast({
        title: '加载失败，请稍后再试',
        icon: 'none',
        duration: 3000
      })
    }
    const {lat, lon, province, city, county} = this.data
    getWeather(lat, lon)
      .then((res) => {
        wx.hideLoading()
        if (typeof cb === 'function') {
          cb()
        }


        if (res.result) {
          this.render(res.result)
        } else {
          fail()
        }
      })
      .catch(fail)
  },

  // 处理逆地址
  getAddress(lat, lon, name) {
    wx.showLoading({
      title: '定位中',
      mask: true
    })
    let fail = (e) => {
      // console.log(e)
      this.setData({
        address: name || '北京市海淀区西二旗北路'
      })
      wx.hideLoading()

    }
    geocoder(
      lat,
      lon,
      (res) => {
        wx.hideLoading()
        let result = (res.data || {}).result
        // console.log(1, res, result)
        if (res.statusCode === 200 && result && result.address) {
          let {address, formatted_addresses, address_component} = result
          if (formatted_addresses && (formatted_addresses.recommend || formatted_addresses.rough)) {
            address = formatted_addresses.recommend || formatted_addresses.rough
          }
          let {province, city, district: county} = address_component
          this.setData({
            province,
            county,
            city,
            address: name || address
          })
          this.getWeatherData()
        } else {
          //失败
          fail()
        }
      },
      fail
    )
  },

  // 更新 data 数据，调用 getAddress
updateLocation(res) {
    let {latitude: lat, longitude: lon, name} = res
    let data = {
      lat,
      lon
    }
    if (name) {
      data.address = name
    }
    this.setData(data)
    this.getAddress(lat, lon, name)
  },


  getLocation() {
    // 获取经纬度
    wx.getLocation({
      type: 'gcj02',
      success: this.updateLocation,
      fail: (e) => {
        // console.log(e)
        this.openLocation()
      }
    })
  },

  // 检测到失败，则提示用户打开位置权限
openLocation() {
    wx.showToast({
      title: '检测到您未授权使用位置权限，请先开启哦',
      icon: 'none',
      duration: 3000
    })
  },


})
